package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mobstat.StatService;
import com.bumptech.glide.BitmapTypeRequest;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.StaffDetailActivity;
import com.centaline.androidsalesblog.activities.sale.StoreListActivity;
import com.centaline.androidsalesblog.adapter.viewholder.StoreAgentVH;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;

import java.util.ArrayList;

/**
 * 门店经纪人
 * Created by Lihq on 16/2/17.
 */
public class StoreAgentAdapter extends RecyclerView.Adapter<StoreAgentVH> {
    private BitmapTypeRequest<String> requestBuilder;
    private final ArrayList<StaffDo> list;
    private StoreListActivity storeListActivity;

    public StoreAgentAdapter(Context context, BitmapTypeRequest<String> requestBuilder,
                             ArrayList<StaffDo> list) {
        this.storeListActivity = (StoreListActivity) context;
        this.requestBuilder = requestBuilder;
        this.list = list;
        setHasStableIds(true);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getStaffNo().hashCode();
    }


    @Override
    public StoreAgentVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_store_agent, parent, false);
        return new StoreAgentVH(view);
    }

    @Override
    public void onBindViewHolder(StoreAgentVH holder, final int position) {
        final StaffDo staffDo = list.get(position);
        if (GlideLoad.needLoad(true)) {
            requestBuilder
                    .load(staffDo.getImgUrl())
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        } else {
            requestBuilder
                    .load("")
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        }
        holder.atv_name.setText(staffDo.getCnName());
        holder.rl_agent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storeListActivity.startActivity(new Intent(storeListActivity, StaffDetailActivity.class)
                        .putExtra(ExtraConstant.StaffNo, staffDo.getStaffNo()));
                StatService.onEvent(storeListActivity, "K1_5_Click", null);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
